//
//  HomeEqumentVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/6/24.
//  Copyright © 2020 tym. All rights reserved.
//

#import "HomeEqumentVC.h"
#import "HomeEqumentCell.h"
#import "Sizes.h"
#import "CustomGifHeader.h"
#import "UIView+ZFFrame.h"
#import "OpenLockVC.h"
#import "FamilyModel.h"
#import "UserModel.h"
#import "LYEmptyViewHeader.h"
#import "FamilyService.h"
#import "HomeEqumentOneCell.h"
#import "AddDeviceViewController.h"
@interface HomeEqumentVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSMutableArray *equmentArray;
@property(nonatomic,strong) FamilyService *services;
@property(nonatomic,weak) UIButton *addBtn;
@end

@implementation HomeEqumentVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(7.5, 7.5, 0, 7.5);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth ,ScreenHeight - NavigateonHeight - TabbarHeight - 180 ) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.alwaysBounceVertical = YES;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    [_collectionView registerClass:[HomeEqumentCell  class] forCellWithReuseIdentifier:[HomeEqumentCell identifiter]];
    [_collectionView registerNib:[UINib nibWithNibName:@"HomeEqumentCell" bundle:nil] forCellWithReuseIdentifier:[HomeEqumentCell identifiter]];
    [_collectionView registerNib:[UINib nibWithNibName:@"HomeEqumentOneCell" bundle:nil] forCellWithReuseIdentifier:[HomeEqumentOneCell  identifiter]];
    [self.view addSubview:_collectionView];
    
    self.view.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"img_empt_equipment"
     titleStr:@""
    detailStr:@"首页是您的家庭设备和房间设备的主界面快来为您的家庭添加设备吧!"];
    TYMButton *addBtn = [TYMButton btn_Image:@"icon_add2"];
    [self.view addSubview:addBtn];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(iPhoneX?-25:-10);
    }];
    [addBtn addTarget:self action:@selector(addDeviceFunc:) forControlEvents:UIControlEventTouchUpInside];
    self.addBtn = addBtn;
   
    TYMWeak(self);
    MJRefreshHeader *header = [CustomGifHeader headerWithRefreshingBlock:^{
        [weakself getFamilyinfofindfamilyequipments];
    }];
    
    self.collectionView.mj_header = header;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLockState:) name:@"homeEqumentupdateLockState" object:nil];
}

//更新锁的状态
-(void)updateLockState:(NSNotification *) notification{
    [self
     getFamilyinfofindfamilyequipments];
}

-(void)getFamilyinfofindfamilyequipments{
    
    if ([UserTool getCurrentFamily].familyRole == 1) {
        self.addBtn.hidden = NO;
    }else{
        self.addBtn.hidden = YES;
    }
    TYMWeak(self);
    if (self.equmentArray.count==0) {
        [MBProgressHUD wj_showActivityLoading:@"" toView:self.view];
    }
    [self.services getFamilyAllEquipmentWithFamilyId:[UserTool getCurrentFamily].familyId completion:^(NSError * _Nonnull error, NSArray<EquipmentModel *> * _Nonnull array) {
        [weakself.collectionView.mj_header endRefreshing];
        [MBProgressHUD wj_hideHUDForView:weakself.view];
        if (weakself.equmentArray.count) {
            [weakself.equmentArray removeAllObjects];
        }
        [weakself.equmentArray addObjectsFromArray:array];
        [weakself.collectionView reloadData];
        if (weakself.homeEqumentCountBlock) {
            weakself.homeEqumentCountBlock(weakself.equmentArray.count);
        }
        
        if (weakself.equmentArray.count==0) {
            [weakself.view ly_showEmptyView];
            weakself.view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.9];
            [weakself.view showTopCircularDead];
        }else{
            weakself.view.backgroundColor = [UIColor clearColor];
            [weakself.view ly_hideEmptyView];
        }
    }];
}

-(NSMutableArray *)equmentArray{
    if (_equmentArray == nil) {
        _equmentArray = [[NSMutableArray alloc] init];
    }
    return _equmentArray;
}

-(FamilyService*)services{
    if (_services == nil) {
        _services = [[FamilyService alloc] init];
    }
    return _services;
}

-(IBAction)addDeviceFunc:(id)sender{
    
    AddDeviceViewController *addDic = [[AddDeviceViewController alloc] init];
    [self.navigationController pushViewController:addDic animated:YES];
}

//分区，组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//每一分区的单元个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.equmentArray.count;
}

//集合视图单元格大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.equmentArray.count <= 2) {
       return [HomeEqumentOneCell cellHeight];
    }
    return [HomeEqumentCell cellHeight];
}

//单元格复用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.equmentArray.count <= 2) {
        HomeEqumentOneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeEqumentOneCell identifiter] forIndexPath:indexPath];
        cell.equmentModel = self.equmentArray[indexPath.row];
        return cell;
        
    }else{
        HomeEqumentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[HomeEqumentCell identifiter] forIndexPath:indexPath];
        cell.equimModel = self.equmentArray[indexPath.row];
        return cell;
    }
}

//被选中的单元格
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    OpenLockVC *openVC = [[OpenLockVC alloc] initWithEquipment:_equmentArray[indexPath.row]];
    [self.presentVC.navigationController pushViewController:openVC animated:YES];
}

- (void)dealloc {
    
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"homeEqumentupdateLockState" object:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
